cmake_minimum_required(VERSION 3.10)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

project(StdStation LANGUAGES CXX C)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC_SEARCH_PATHS ${CMAKE_CURRENT_SOURCE_DIR}/ui)

find_package(Qt5 COMPONENTS
    Widgets
    Sql
    Network
    Svg
    Concurrent
    SerialBus
    SerialPort
    Charts
    REQUIRED
)

# Specify MSVC UTF-8 encoding   
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")

# 添加 Qt 资源文件
set(PROJECT_RESOURCES
    ${CMAKE_CURRENT_SOURCE_DIR}/res.qrc
)

# 设置 Windows 应用程序图标 RC 文件
if(WIN32)
    set(WINDOWS_RC_FILE ${CMAKE_CURRENT_SOURCE_DIR}/resource/appicon.rc)
endif()

# 根据构建类型决定是否添加 WIN32 (仅 Windows)
set(APP_TYPE "") # 默认无类型 (带控制台)
if(WIN32)
    # 如果不是 Debug 模式，则设置为 WIN32 (无控制台 GUI)
    if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
        set(APP_TYPE WIN32)
    endif()
endif()

# 将资源文件添加到项目中
qt_add_resources(RESOURCE_SRCS ${PROJECT_RESOURCES})

# 定义CODE路径
set(CODE_DIRS
    ${CMAKE_CURRENT_SOURCE_DIR}/code
    ${CMAKE_CURRENT_SOURCE_DIR}/code/common
    ${CMAKE_CURRENT_SOURCE_DIR}/code/core
    ${CMAKE_CURRENT_SOURCE_DIR}/code/devices
    ${CMAKE_CURRENT_SOURCE_DIR}/code/modbus
    ${CMAKE_CURRENT_SOURCE_DIR}/code/sql
    ${CMAKE_CURRENT_SOURCE_DIR}/code/update
    ${CMAKE_CURRENT_SOURCE_DIR}/code/upload
    ${CMAKE_CURRENT_SOURCE_DIR}/code/utils
    ${CMAKE_CURRENT_SOURCE_DIR}/code/window/component
    ${CMAKE_CURRENT_SOURCE_DIR}/code/window/inc
    ${CMAKE_CURRENT_SOURCE_DIR}/code/window/src
)
#添加头文件及源文件
foreach(dir ${CODE_DIRS})
    file(GLOB temp_headers ${dir}/*.h)
    list(APPEND headers ${temp_headers})
    include_directories(${dir})
    aux_source_directory(${dir} srcs)
endforeach()

#添加UI文件
file(GLOB uis ui/*.ui)

#添加库
add_subdirectory(libs/qtpromise)
add_subdirectory(libs/json)
add_subdirectory(libs/modbus)

#添加测试程序
add_subdirectory(demo/spike_recovery_test)

#添加可执行文件
add_executable(${PROJECT_NAME}
    ${APP_TYPE} # 在非 Debug 的 Windows 构建中添加 WIN32
    ${srcs}
    ${headers}
    ${uis}
    ${RESOURCE_SRCS}
    ${WINDOWS_RC_FILE} # 添加 RC 文件 (conditionally set earlier)
)

# 链接Windows Socket库
if(WIN32)
    target_link_libraries(${PROJECT_NAME} PRIVATE ws2_32)
endif()

#链接库
target_link_libraries(${PROJECT_NAME} PRIVATE
    Qt5::Widgets
    Qt5::Sql
    Qt5::Network
    Qt5::Svg
    Qt5::Concurrent
    Qt5::SerialBus
    Qt5::SerialPort
    Qt5::Charts
    qtpromise
)
